//  Copyright (c) 2010 Mahesh Sharma,Matt MacDonald
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.

package org.prx.prp.view;

import org.prx.prp.R;
import org.prx.prp.utility.DatabaseAdapter;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AlphabetIndexer;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.SimpleCursorAdapter;

public class ABCListActivity extends ListActivity {
	SimpleCursorAdapter adapter;
	private Context mContext;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {
			mContext = this.getApplicationContext();
			super.onCreate(savedInstanceState);
			getListView().setHorizontalFadingEdgeEnabled(false);
			getListView().setBackgroundColor(Color.parseColor("#FFFFFF"));
			getListView().setCacheColorHint(Color.parseColor("#FFFFFF"));
			getListView().setFastScrollEnabled(true);
			Cursor mCursor = DatabaseAdapter
					.executeQuery(
							"SELECT UPPER(TRIM(PROGRAMS.TITLE)) AS PROGRAM_INDEX,TRIM(PROGRAMS.TITLE) AS PROGRAM_TITLE,PROGRAMS.ID AS PROGRAM_ID, PROGRAMS.ID as _id FROM PROGRAMS WHERE PROGRAMS.PODCAST IS NOT NULL AND PROGRAMS.PODCAST !='' AND PROGRAMS.PODCAST!='NULL' ORDER BY PROGRAM_INDEX",
							null);
			startManagingCursor(mCursor);
			adapter = new IndexedStationAdapter(this,
					R.layout.list_single_item_view, mCursor,
					new String[] { "PROGRAM_TITLE" }, new int[] { R.id.text1 },
					"PROGRAM_INDEX");
			ListView listview = getListView();
			setContentView(listview);
			listview.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					Cursor cursorOfSelectedItem = (Cursor) adapter
							.getItem(position);
					int programIDOfSelectedItem = cursorOfSelectedItem
							.getInt(cursorOfSelectedItem
									.getColumnIndex("PROGRAM_ID"));
					Bundle paramsBundle = new Bundle();
					paramsBundle.putInt("PROGRAM_ID", programIDOfSelectedItem);
					Intent intentToFire = new Intent(mContext,
							ProgramInfoView.class);
					intentToFire.putExtras(paramsBundle);
					startActivity(intentToFire);
				}
			});
			listview.setTextFilterEnabled(true);
			adapter.setFilterQueryProvider(new FilterQueryProvider() {
				public Cursor runQuery(final CharSequence substring) {
					Cursor cursor = DatabaseAdapter
							.executeQuery(
									"SELECT TRIM(PROGRAMS.TITLE) AS PROGRAM_TITLE,PROGRAMS.ID AS PROGRAM_ID, PROGRAMS.ID as _id FROM PROGRAMS WHERE PROGRAMS.PODCAST IS NOT NULL AND PROGRAMS.PODCAST !='' AND PROGRAMS.PODCAST!='NULL' AND UPPER(PROGRAM_TITLE) LIKE ('%'||UPPER('"
											+ substring
											+ "')||'%') ORDER BY TRIM(PROGRAMS.TITLE)",
									null);
					return cursor;
				}
			});

			setListAdapter(adapter);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private class IndexedStationAdapter extends SimpleCursorAdapter implements
			SectionIndexer {
		AlphabetIndexer indexer;

		public IndexedStationAdapter(Context context, int layout,
				Cursor cursor, String[] from, int[] to, String ColumnForKey) {
			super(context, layout, cursor, from, to);
			indexer = new AlphabetIndexer(cursor,
					cursor.getColumnIndex(ColumnForKey),
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
		}

		@Override
		public int getPositionForSection(int arg0) {
			return indexer.getPositionForSection(arg0);
		}

		@Override
		public int getSectionForPosition(int arg0) {
			return indexer.getSectionForPosition(arg0);
		}

		@Override
		public Object[] getSections() {
			return indexer.getSections();
		}
	}
}
